package com.example.ymm.modules1.service.impl.hl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.dkn.DknStockProduct;
import com.example.ymm.modules1.mapper.hl.DknStockProductMapper;
import com.example.ymm.modules1.query.hl.DknStockProductQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.ImportStockProductDto;
import com.example.ymm.modules1.service.hl.DknStockProductService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
//@Transactional
@Slf4j
@DS("slave2")
public class DknStockProductServiceImpl extends BaseServiceImpl<DknStockProductMapper, DknStockProduct> implements DknStockProductService {

    @Override
    public TableList<DknStockProduct> list(DknStockProductQueryCriteria criteria, YmPageDto pageDto) {
        getPage(pageDto);
        List<DknStockProduct> arrs = queryAll(criteria);
        PageInfo<DknStockProduct> pageInfo = new PageInfo<>(arrs);
        TableList<DknStockProduct> tableList = new TableList(pageInfo);
        return tableList;
    }

    public List<DknStockProduct> queryAll(DknStockProductQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DknStockProduct.class, criteria);
        predicate.orderByDesc("create_time");
        List<DknStockProduct> result = baseMapper.selectList(predicate);
        return result;
    }

    public DknStockProduct findByMnoAndKuWeiName(String mno, String kuWeiName) {
        DknStockProduct result = null;
        DknStockProductQueryCriteria criteria = new DknStockProductQueryCriteria();
        criteria.setMno(mno);
        criteria.setKuWeiName(kuWeiName);
        List<DknStockProduct> dknStockProducts = queryAll(criteria);
        if (dknStockProducts.size() > 0) {
            result = dknStockProducts.get(0);
        }
        return result;
    }


    @Override
    public List<String> importStockProduct(ImportStockProductDto dto) {
        List<DknStockProduct> list = dto.getList();
        if(UtilValidate.isEmpty(list)){
            throw new YmException("参数异常!");
        }
        List<String> msg = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            DknStockProduct mapping=list.get(i);
            if(UtilValidate.isEmpty(mapping.getMno())){
                msg.add("第"+(i+1)+"行 ms物料 编码不能为空！");
            }
            if(UtilValidate.isEmpty(mapping.getKuWeiName())){
                msg.add("第"+(i+1)+"行 库位名称 不能为空！");
            }
            DknStockProduct product = findByMnoAndKuWeiName(mapping.getMno(), mapping.getKuWeiName());
            if(UtilValidate.isNotEmpty(product)){
                mapping.setId(product.getId());
                mapping.setCreateTime(product.getCreateTime());
            }
        }

        if(UtilValidate.isEmpty(msg)){
            saveOrUpdateBatch(list);
        }
        return msg;
    }
}
